package cn.tedu.util;

import java.sql.*;

public class JDBCUtil {

    /**
     * 注册驱动
     */

    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    /**
     * 连接数据库
     * @return
     * @throws SQLException
     */
   public static Connection getConnection() throws SQLException {
       String url= "jdbc:mysql://localhost:3306/gdb2109";
       Connection con = DriverManager.getConnection(url, "root", "root");
       return con;
   }

    /**
     * 关闭资源
     */
    public static void getClose(Connection con,PreparedStatement ps,ResultSet rs){
        if (con == null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (ps == null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (rs == null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

    //测试一下数据库是否连接成功
    public static void main(String[] args) throws SQLException {
        Connection con = JDBCUtil.getConnection();
        System.out.println(con);
    }

}
